package com.project.rfidpage.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.project.rfidpage.dto.LicenseDto;
import com.project.rfidpage.dto.LicenseParam;
import com.project.rfidpage.entity.License;
import com.project.rfidpage.mapper.LicenseMapper;
import com.project.rfidpage.service.LicenseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class LicenseServiceImpl implements LicenseService {

    @Resource
    private LicenseMapper licenseMapper;

    @Override
    @DS("master")
    public List<LicenseDto> getListForPage(LicenseParam param) {
        return licenseMapper.selectListByUid(param);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void insert(List<License> list) {
        for (License entity : list) {
            licenseMapper.insertSelective(entity);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void update(List<License> list) {
        for (License entity : list) {
            Example example = new Example(License.class);
            Example.Criteria c = example.createCriteria();
            c.andEqualTo("applytype", entity.getApplytype());
            entity.setApplytype(null);
            licenseMapper.updateByExampleSelective(entity, example);
        }
    }

    @Override
    @DS("master")
    public List<License> getEmptyList(Integer type) {
        Example example = new Example(License.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("makestatus", 0);
        if (type == 0) {
            c.andIsNull("cardid");
        }
        example.orderBy("applytype");
        return licenseMapper.selectByExample(example);
    }
}
